﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.Common;
using System.Text.RegularExpressions;
using System.Web;
using TVGuide.DataAccess;
using TVGuide.DataAccess.Common;

namespace TVGuide.Parser
{
    class H2Parser : IChannelParser
    {
        public Guid ChannelId()
        {
            return Guid.Empty;
        }

        
        /// <summary>
        ///     Parses H2 TV programs for current date
        /// </summary>
        /// 
       
        List<Program> IChannelParser.ParseProgram()
        {
            return ParseProgram(DateTime.Today);
        }

        /// <summary>
        ///     Parses H TV programs for current date
        /// </summary>
        /// <param name="date"> 
        ///     If we want to grab schedule for other dates that is specified by this DateTime instance
        /// </param>
        public List<Program> ParseProgram(DateTime date)
        {
            List<Program> TVProgList = new List<Program>();
            string h2String = HTTPClient.GetSiteContent(ChannelUrls.H2_ARM_URL.Replace("arm","eng"), Encoding.Default, this.ChannelName());
            Regex ContentReg = new Regex("</table>");
            int StartInd = 0; 
            int EndInd = 0;                   
            StartInd = Regex.Match(h2String, @"\d{2}.\d{2}.\d{4}").Index;
            if (StartInd > 0)
                EndInd = ContentReg.Match(h2String, StartInd).Index;
                
            if (EndInd > StartInd)
            {
                Regex ListRegWithoutTag = new Regex(@"\d{2}.\d{2}[^<]+");       
                string ContentStr = h2String.Substring(StartInd + 10, EndInd - StartInd);

                // removeing <span>...</span>, replaceing &quot,&nbsp;
                ContentStr = ContentStr.Replace(@"&quot;", "\"");
                ContentStr = ContentStr.Replace(@"&nbsp;", "");
                ContentStr = ContentStr.Replace(Regex.Match(ContentStr, "<span.+?>").Value, "");
                ContentStr = ContentStr.Replace(Regex.Match(ContentStr, "</span>").Value, "");

                MatchCollection Mcoll = ListRegWithoutTag.Matches(ContentStr);
        
                foreach (Match mm in Mcoll)
                {
                    Program prog = new Program();
                    prog.Name = mm.Value.Substring(Regex.Match(mm.Value, @"\d{2}.\d{2}").Value.Length).Trim();
                    prog.ChannelId = (int)Enums.Channels.H2;
                    try
                    {
                        prog.Date = new DateTime(date.Year, date.Month, date.Day, Int32.Parse(Regex.Match(mm.Value, @"\d{2}.\d{2}").Value.Substring(0, 2)), Int32.Parse(Regex.Match(mm.Value, @"\d{2}.\d{2}").Value.Substring(3, 2)), 0);
                    }
                    catch (Exception e)
                    {
                        string ErrorString = e.Message;
                    }
                   
                    TVProgList.Add(prog);
                }
            }

            if (TVProgList.Count == 0)
            {
                TVGuide.Common.Utils.SendNotificationMail("Empty program list for " + this.ChannelName() + " program");
                return TVProgList;
            }
            return TVProgList;
        }
        
        public string ChannelName()
        {
            return "H2";
        }




        public List<Program> ParseProgramForWeek()
        {
            throw new NotImplementedException();
        }
    }
}
